package test.davidlauzon.activerecord;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import davidlauzon.activerecord.connection.AndroidSqliteAdapter;
import davidlauzon.activerecord.connection.ConnectionAdapter;
import davidlauzon.activerecord.nodes.field.Field;
import davidlauzon.activerecord.recordset.RecordSet;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class ActiveRecordActivity extends Activity
{
    private static final String TAG = "Test";
    private ConnectionAdapter _adapter;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
            _adapter = new AndroidSqliteAdapter( getApplicationContext(), AndroidDB.NAME, 1
            );
            AndroidDB.getInstance().setConnection( _adapter );
            AndroidDB.getInstance().setTables( AndroidDB.TABLES );
            printRecords();
            
            // Create a record
            Map<Field, Object> values = new HashMap<Field, Object>();
            values.put(MyPerson.NAME, "Priya Rey");
            values.put(MyPerson.AGE,  32);
            values.put(MyPerson.SIZE, 42.2436f);
            
            MyPerson person = MyPerson.create(values);
            Log.i(TAG, "Should have created (new=" + person.isNewRecord() + ") a Person: " + person.toString());
            printRecords();
            
            // Updating
            person.set(MyPerson.SIZE, 69.69f);
            if (person.save())
                Log.i(TAG, "Person updated.");
            else
                Log.i(TAG, "Person NOT updated.");
            printRecords();
            
            // Deleting
            if (person.delete())
                Log.i(TAG, "Person deleted.");
            else
                Log.i(TAG, "Person NOT deleted.");
            printRecords();
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        //testReflection();
    }
    
    private void printRecords() {
        try {
            String    sql = "SELECT * FROM people;";
            RecordSet set =_adapter.executeQuery(sql);
            
            long   id;
            String name;
            float  size;
            int    age;
            
            Log.i(TAG, sql);
            while (set.next()) {
                id   = set.getLong(0);
                name = set.getString(1);
                size = set.getFloat(2);
                age  = set.getInt(3);
                
                Log.i(TAG, String.format("\t#%d : %s - %f @ %d", id, name, size, age));
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    private void testReflection() {
        Log.i("Test", "ChildOne.getStaticClass : " + ChildOne.getStaticClass());
        Log.i("Test", "ChildTwo.getStaticClass : " + ChildTwo.getStaticClass());
    }
  
}

class Parent
{
    static public Parent create() {
        String className = getStaticClass();
        return null;
    }
    
    static public String getStaticClass() { return reallyGetStaticClass(); }
    
    static public String reallyGetStaticClass() {
        // * METHOD ONE
        // return new Object() { }.getClass().getEnclosingClass().getName();  // => Parent
        
        // * METHOD TWO
        Throwable t = new Throwable();
        t.fillInStackTrace();
        StackTraceElement[] elements = t.getStackTrace();
        return t.getStackTrace()[1].getClassName();
    }
}

class ChildOne extends Parent
{
    static public String getStaticClass() { return reallyGetStaticClass(); }
}

class ChildTwo extends Parent
{
    static public String getStaticClass() { return reallyGetStaticClass(); }
}